package com.tuanzi.loan.web.bean.xhh.settlementsystem;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.constants.DictionaryCode;
import com.tuanzi.loan.business.entity.xhh.settlementsystem.XhhSettlementCompany;
import com.tuanzi.loan.business.service.xhh.settlementsystem.XhhSettlementCompanyService;
import com.tuanzi.loan.business.service.xhh.settlementsystem.XhhSettlementIncomeService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.web.configuration.JSFInfoBean;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;
import java.util.Objects;

/**
 * @author caiwanlin
 * @create 2018-08-16 20:36
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class XhhSettlementCompanyInfoBean extends JSFInfoBean<XhhSettlementCompany> {

    @Autowired
    private XhhSettlementCompanyService xhhSettlementCompanyService;

    @Autowired
    private XhhSettlementIncomeService xhhSettlementIncomeService;

    private String originalCompanyName = "";

    @PostConstruct
    private void init() {
        //生效时间设置
        String id = getParamFromRequest("id");
        if (StringUtils.isNotBlank(id)) {
            $ = xhhSettlementCompanyService.findOne(Long.parseLong(id));
        } else {
            $ = new XhhSettlementCompany();
            originalCompanyName = $.getCompanyName();
            changeStatusToAdd();
        }
    }

    public String onSave() {
        if (validate() == false) return null;
        if ($.getId() == null) {//新增公司时，默认公司的预付金额为0，消耗金额为0
            $.setPrepayAmount(0.0);
            $.setExpendAmount(0.0);
        }
        cleanNotUseData();
        updateIncomeCompanyName();
        $ = xhhSettlementCompanyService.saveOrUpdate($);
        changeStatusToView();
        addSuccessfullyMessage();
        return redirect("/views/xhh/settlementsystem/company-list.xhtml");
    }

    private void updateIncomeCompanyName() {
        if (null != $.getId() && !originalCompanyName.equals($.getCompanyName())) {
            xhhSettlementIncomeService.updateCompanyNameByCompanyId($.getId(), $.getCompanyName());
        }
    }

    public boolean validate() {
        XhhSettlementCompany dbCompany = xhhSettlementCompanyService.findByCompanyName($.getCompanyName());
        if (null != dbCompany && !dbCompany.getId().equals($.getId())) {
            addErrorMessage("主体名称已存在");
            return false;
        }

        Boolean isBill = $.getIsBill();
        if (isBill) {
            if ($.getBillContent() == null) {
                addErrorMessage("开票内容为空");
                return false;
            }
            if ($.getBillType() == null) {
                addErrorMessage("发票类型为空");
                return false;
            }
            if (StringUtils.isBlank($.getBankAccount())) {
                addErrorMessage("开户账户为空");
                return false;
            }
			if (Objects.isNull($.getBillProject())) {
				addErrorMessage("所属项目为空");
				return false;
			}
        }
        return true;
    }

    public void cleanNotUseData() {
        if ($.getIsBill() == false) {
            $.setBillContent(null);
            $.setBillType(null);
            $.setTaxNumber(null);
            $.setBankAddress(null);
            $.setBankNumber(null);
            $.setBillAddress(null);
            $.setReceiveAddress(null);
            $.setReceiveName(null);
            $.setReceivePhone(null);
        }
    }

    public String onReturn(String url) {
        return redirect(url, getParam());
    }

    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(DictionaryCode.XHH_SETTLEMENT_BILLING_CONTENT,
                DictionaryCode.XHH_SETTLEMENT_BILL_TYPE,  DictionaryCode.XHH_SETTLEMENT_BILLING_PROJECT);
    }
}
